// pages/modelStore/modelDetail/index.js
import {
  modelApi
} from "../../../api/index.js"
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: "",
    detailData: null,
    navHeadH: 0,
    footerH: 0,
    imgHight: "",
    current: 1,
    spaceType: ['室内', '景观', '建筑'],
    space: "",
    price: "", //价格
    headObj: {}
  },
  // 获取详情
  async getData(id) {
    let res = await modelApi.getModelDetail({
      id
    })
    let {
      feeType,
      paymentMethod,
      paymentAmount,
      creatorId,
      userName,
      avatar,
      isModelCollection
    } = res
    if (feeType === 2) { //收费
      this.setData({
        price: paymentMethod === 1 ? `￥${paymentAmount}/1次` : `${paymentAmount}算力值/1次`
      })
    } else { //免费
      this.setData({
        price: '免费'
      })
    }
    console.log('详情', res);
    this.setData({ //设置header
      headObj: {
        userId: creatorId,
        userName,
        avatar,
        isCollect: isModelCollection
      }
    })
    this.setData({
      space: this.data.spaceType[Number(res.bottomMoldType) - 1]
    })
    this.setData({
      detailData: res
    })
    // this.data.detailData = res

  },
  // 改变swiper页码
  swiperChange(e) {
    const current = e.detail.current + 1; // 因为索引是从0开始的，所以需要加1
    this.setData({
      current,
    });
  },
  // 去app
  toApp() {
    console.log(111111111111111);
    wx.navigateTo({
      url: '/pages/ai/downloadApp/index',
    })
  },
  // 查看大图
  onClick(e) {
    wx.previewImage({
      current: e.currentTarget.dataset.url, // 当前显示图片的http链接
      urls: this.data.detailData.coverImage, // 需要预览的图片http链接列表
    });
  },
  // 复制模型编号
  copyNum(e) {
    // console.log('复制模型',e.currentTarget.dataset.num);
    wx.setClipboardData({
      data: e.currentTarget.dataset.num,
      success() {
        wx.showToast({
          title: '复制成功',
          icon: 'success'
        });
      }
    });
  },
  // 登录弹窗
  shudLogin(e) {
    const popup = this.selectComponent("#popupid");
    // console.log('popup',popup);
    popup.setData({
      visible: e || false,
    });
  },
  // 关注
  async Ifollow() {
    console.log('关注作者');
  },
  //生命周期函数--监听页面加载
  onLoad(options) {
    this.getData(options.id)
  },
  onShow() {
    this.shudLogin()
  },
  //生命周期函数--监听页面初次渲染完成
  onReady() {
    wx.createSelectorQuery().select("#detailHeader").boundingClientRect((rect) => {
      if (rect && rect.height) {
        this.setData({
          navHeadH: parseInt(rect.height)
        })
      }
    }).exec()
    // wx.createSelectorQuery().select("#footer").boundingClientRect((rect) => {
    //   console.log('footer',rect);
    //   if (rect && rect.height) {
    //     this.setData({
    //       footerH: parseInt(rect.height)
    //     })
    //   }
    // }).exec()
  },
  onShareAppMessage() {
    // console.log('分享');
    let _this = this
    return {
      title: _this.data.detailData.name
    };
  }
})